import java.util.Arrays;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description: 给定一个数组，将数组中的元素向右移动 k 个位置，其中 k 是非负数.
 * User: 15085202591
 * Date: 2022-11-25
 * Time: 23:33
 */
//旋转数组，即：数组中每个元素向右旋转k位
public class Rotate{
    public static int[] rotate(int[] nums,int k){
        k=k%nums.length;
        //先拷贝要旋转到前面来的
        int[] rightpart= Arrays.copyOfRange(nums,nums.length-k,nums.length);
        //再进行旋转（旋转到后面的length-k位）
        System.arraycopy(nums,0,nums,k,nums.length-k);
        System.arraycopy(rightpart,0,nums,0,k);
        return nums;
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int[] nums=new int[]{1, 2, 3, 4,5,6,7};
        System.out.print("输入：");
        System.out.println(Arrays.toString(nums));
        System.out.println("输入要移动位置：");
        int k = sc.nextInt();
        System.out.print("输出：");
        System.out.println(Arrays.toString(rotate(nums, k)));
    }
}

